<template>
	<view class="content-img">
		<eui-nav-bar title="详情" color="#000" rightIcon="" />
	</view>
	<view class="main-view padding-all-20">
		<scroll-view class="background-color-fff border-radius-20 padding-all-28" scroll-y
			:style="{width:scrollWidth,height:scrollHeight}">
			<view class="font-size-36 font-weight-bold">{{info.c_title}}</view>
			<view class="flex margin-top-20 align-items-center">
				<image :src="personImg" @error="setErrorImg" mode="scaleToFill"
					style="width: 56rpx;height: 56rpx;border-radius: 50%;"></image>
				<view class="margin-left-16 font-size-24">{{info.c_mk_username}}</view>
				<view class="margin-left-16 font-size-20 color-888">{{info.c_dt}}</view>
			</view>
			<view class="margin-top-40">
				<view class="email-html" v-html="c_html"></view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup type="ts">
	import { onLoad,onReady } from '@dcloudio/uni-app';
	import { ref } from 'vue';
	import Enjoy from '@/common/enjoy';
	import service from '@/common/service';
	import eventBus from '@/pages/MainViews/eventBus';
	import { useMainStore } from '@/store/modules/store';
	import { useLoginStore } from '@/store/modules/useLoginStore';
	
	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	const scrollWidth = ref((uni.getSystemInfoSync().windowWidth-48) + 'px');
	const scrollHeight = ref((uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight-92) + 'px');
	const statusBarHeight = ref(uni.getSystemInfoSync().statusBarHeight);
	
	const info = ref({});
	const personImg = ref('');
	const c_html = ref('');
	
	const setErrorImg = () =>{
		personImg.value ='/static/img/no_image.png';
	}
	const EnjoyBillQuery = c_id =>{
		service.enjoyShowLoading('正在加载...');
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40企业新闻",
		        ObjectData: {
					c_id,
					c_userno:mainStore.loginInfo.sUser,
				},
		        MethodName: "EnjoyBillQuery",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
		        if (!res.data.HasException) {
					const ObjectData = res.data.ObjectData;
					// console.log("ObjectData: " + JSON.stringify(ObjectData));
					if(ObjectData != null && ObjectData.ReturnObject.length > 0) {
						info.value = {
							...ObjectData.ReturnObject[0],
						};
						DocGetContent(ObjectData.ReturnObject[0].c_guid);
						const imgUrl = LoginStore.EnjoyMain.replace('/Enjoy/Service', '');
						personImg.value = imgUrl + '/File/DownLoadImage/' + ObjectData.ReturnObject[0].c_to_userno;
					}
					updateReadStatus(info.value)
		        } else {
		            service.enjoyShowToast(res.data.Exception.Message)
		        }
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	const updateReadStatus = (e) => {
		if(e.c_rev_status =='未阅' || e.c_rev_status =='N') {
			service.enjoyShowLoading('正在处理...');
			let request = {
			    url: LoginStore.EnjoyMain,
			    data: {
			        UniqueKey: "管易通40企业新闻",
			        ObjectData: e.msgCid+";"+mainStore.loginInfo.sUser,
			        MethodName: "updateReadStatus",
			    },
			}
			service.request(request, (res) => {
			    if (res && res.data) {
			        if (!res.data.HasException) {
						eventBus.emit('refresh-NEWSList');
						setTimeout(() => {
							SetCurrentPages();
						}, 300)
			        } else {
			            service.enjoyShowToast(res.data.Exception.Message)
			        }
			    }
				service.enjoyHideLoading();
			}, () => {
				service.enjoyHideLoading();
			    console.log('失败')
			});
		}
	}
	// 获取内容
	const DocGetContent = c_guid =>{
		service.enjoyShowLoading('正在加载...');
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40公共",
		        ObjectData: c_guid,
		        MethodName: "DocGetContent",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
		        if (!res.data.HasException) {
					const ObjectData = res.data.ObjectData;
					c_html.value = ObjectData;
		        } else {
		            service.enjoyShowToast(res.data.Exception.Message)
		        }
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	
	const SetCurrentPages = () => {
		try {
			const pages = getCurrentPages();
			if (pages.length >= 2) {
				const prevPage = pages[pages.length - 2];
				const prevPageRoute = prevPage.route;
				if (prevPageRoute === 'pages/OaViews/News/index') {
					prevPage.$vm.$.exposed.refreshEventBus();
				}
			}
		} catch (e) {
			console.error('SetCurrentPages 执行过程中出现错误:', e);
		}
	};

	onLoad(e=>{
		if(e.item){
			const item = JSON.parse(e.item)
			EnjoyBillQuery(item.c_id);
		}
	})
	defineExpose({
		SetCurrentPages
	})
</script>

<style lang="scss" scoped>
	@import '@/main.scss';

	.content-img {
		background-color: #FFFFFF;
		background-repeat: no-repeat;
		width: 100%;
	}

	.main-view {
		flex: 1;
		flex-direction: column;
		padding: 20rpx;
		background-color: #f2f2f2;
	}

	.flex {
		display: flex;
	}

	.align-items-center {
		align-items: center;
	}

	.padding-all-20 {
		padding: 20rpx;
	}

	.background-color-fff {
		background-color: #fff;
	}

	.border-radius-20 {
		border-radius: 20rpx;
	}

	.padding-all-28 {
		padding: 28rpx;
	}

	.font-size-36 {
		font-size: 36rpx;
	}

	.font-size-26 {
		font-size: 26rpx;
	}

	.font-size-24 {
		font-size: 24rpx;
	}

	.font-size-20 {
		font-size: 20rpx;
	}

	.font-weight-bold {
		font-weight: bold;
	}

	.margin-top-20 {
		margin-top: 20rpx;
	}

	.margin-top-40 {
		margin-top: 40rpx;
	}

	.margin-left-16 {
		margin-left: 16rpx;
	}

	.color-888 {
		color: #888;
	}
</style>